package org.example.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/response/html")
public class ResponseHTML extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //返回一个动态的网页：userId=1，显示欢迎你张三，其他的，欢迎你李四
        String useId = req.getParameter("useId");
        // 初始化一个字符串模板，%s是可以使用变量来替换
        String html = "<p>欢迎你，%s</p>";
        String name = "张三";
        if (!"1".equals(useId)) {
            name = "李四";
        }
        //字符串格式化：替换%s
        String body = String.format(html,name);
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(body);
    }
}
